/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jepi;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.*;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author liz
 */
public class Grafico {

    private BufferedImage _image;//para la imagen en memoria
    private JFreeChart grafico;// el grafico
    private Dimension d;//dimension del grafico

    public Grafico() {
    }
    /* Crea el grafico */

    public JPanel crear_grafico_de_flujos(Dimension d, float[] serie1, float[] serie2, int n) {
        this.d = d;

        XYSeries s1 = new XYSeries("Flujo");
        XYSeries s2 = new XYSeries("Flujo acumulado");

        for (int i = 0; i < n; i++) {
            s1.add(i + 1, serie1[i]);
            s2.add(i + 1, serie2[i]);

        }

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(s1);
        dataset.addSeries(s2);

        JFreeChart grafico = ChartFactory.createXYLineChart("Cambio del flujo y flujo acumulado", "Periodo", "Dolares", dataset, PlotOrientation.VERTICAL, true, true, true);
        XYPlot plot= (XYPlot)grafico.getPlot();
        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
        renderer.setShapesVisible(true);
        renderer.setDrawOutlines(true);
        renderer.setUseFillPaint(true);


        //_image = grafico.createBufferedImage(this.d.width, this.d.height);
        System.err.println("grafico creado");
        return new ChartPanel(grafico);
    }

    public JPanel crear_grafico_costo_beneficio(Dimension d, float[] costo, float[] beneficio, int n) {
        this.d = d;

        XYSeries s1 = new XYSeries("Costo");
        XYSeries s2 = new XYSeries("Beneficio");

        for (int i = 0; i < n; i++) {
            s1.add(i + 1, costo[i]);
            s2.add(i + 1, beneficio[i]);

        }

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(s1);
        dataset.addSeries(s2);

        JFreeChart grafico = ChartFactory.createXYBarChart("Analisis de flujo de efectivo", "Periodo", false, "Dolares", dataset, PlotOrientation.VERTICAL, true, true, true);

        //JFreeChart grafico = ChartFactory.createXYStepChart("Analisis de magnitud de los costos e ingresos","Periodo","Dolares",dataset,PlotOrientation.VERTICAL,true,true,false);


        //_image = grafico.createBufferedImage(this.d.width, this.d.height);
        System.err.println("grafico creado");
         return new ChartPanel(grafico);
    }

    /* carga la imagen que esta en memoria en el objeto jLabel */
    public void cargar_grafico(JLabel lb) {
        ImageIcon imagenFondo = new ImageIcon(_image);
        lb.setIcon(imagenFondo);
        lb.repaint();
    }
}



